package com.zx.ecode.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.encoding.EncodingHandler;
import com.zx.ecode.DataExampleUtil;
import com.zx.ecode.R;
import com.zx.ecode.model.entity.ECodeBean;
import com.zx.ecode.model.entity.UserBean;
import com.zx.ecode.ui.activity.MainActivity;
import com.zzp.basepresenter.ui.fragment.BaseFragment;
import com.zzp.basepresenter.util.DateUtils;
import com.zzp.basepresenter.util.LayoutUtil;
import com.zzp.basepresenter.util.LoggerUtils;
import com.zzp.basepresenter.util.TextUtils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexFragment extends BaseFragment {
    private static IndexFragment instance;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_update_time)
    TextView tvUpdateTime;
    @BindView(R.id.iv_e_code)
    ImageView ivECode;
    @BindView(R.id.tv_risk)
    TextView tvRisk;
    @BindView(R.id.tv_trip)
    TextView tvTrip;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id_number)
    TextView tvIdNumber;

    public static IndexFragment getInstance() {
        if (instance == null) {
            synchronized (IndexFragment.class) {
                if (instance == null) {
                    instance = new IndexFragment();
                }
            }
        }
        return instance;
    }

    @Override
    public void bindButter(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_index;
    }

    @Override
    protected void lazyLoad(Activity context, View view) {
        tvTitle.setLayoutParams(LayoutUtil.getInstance().getRelativeLayoutMarginParams(context, tvTitle));
        initData(DataExampleUtil.getUser_1(MainActivity.user));
    }

    public void initData(UserBean userBean) {
        tvStatus.setText(userBean.getVaccine_status() == 0 ? getString(R.string.unvaccinated) : getString(R.string.vaccinated));
        tvName.setText(URLDecoder.decode(userBean.getName()));
        tvIdNumber.setText(String.format("%s*************%s", userBean.getId_number().substring(0, 1), userBean.getId_number().substring(userBean.getId_number().length() - 1)));
        tvRisk.setText(userBean.getRisk_level() == 1 ? "低风险" : userBean.getRisk_level() == 2 ? "中风险" : userBean.getRisk_level() == 3 ? "高风险" : "");
        String trip = "";
        for (int i = 0; i < userBean.getTrip().size(); i++) {
            LoggerUtils.getInstance().i("行程记录\t\t" + URLDecoder.decode(userBean.getTrip().get(i).getNameWithColor()));
            trip = trip + URLDecoder.decode(userBean.getTrip().get(i).getNameWithColor()) + " → ";
        }
        String tripTitle = userBean.getRisk_level() == 1 ? getString(R.string.trip) :
                userBean.getRisk_level() == 2 ? TextUtils.getInstance().wrapperColor("#FF962F", getString(R.string.trip)) :
                        userBean.getRisk_level() == 3 ? TextUtils.getInstance().wrapperColor("#FF3011", getString(R.string.trip)) : getString(R.string.trip);
        tvTrip.setText(Html.fromHtml(tripTitle + ":" + trip.substring(0, trip.lastIndexOf(" → "))));
        try {
            tvUpdateTime.setText(DateUtils.getInstance().longToString(System.currentTimeMillis(), DateUtils.getInstance().YYYY_MM_DD_HH_MM_SS));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            ECodeBean bean = new ECodeBean();
            bean.setUserBean(userBean);
            bean.setTimes(System.currentTimeMillis());
            ivECode.setImageBitmap(EncodingHandler.createQRCode(bean.ObjectToJson(), 2000,
                    userBean.getRisk_level() == 1 ? 0xFF64AC69 : userBean.getRisk_level() == 2 ? 0xFFFF962F : userBean.getRisk_level() == 3 ? 0xFFFF3011 : 0xFF64AC69));
            LoggerUtils.getInstance().i(bean.ObjectToJson());
        } catch (com.google.zxing.WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {
            if (resultCode == Activity.RESULT_OK) {

            }
        }
    }
}
